package com.shiguo365.market.display.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;


import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WebViewActivity extends BaseActivity {
    private static final String TAG = "WebViewActivity";

    @BindView(R.id.webView)
    WebView mWebView;

    @BindView(R.id.webview_toolbar)
    protected Toolbar toolbar;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        ButterKnife.bind(this);
        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));
        Intent intent =getIntent();
        String url=intent.getStringExtra("url");
        //设置编码
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        //支持js
        mWebView.getSettings().setJavaScriptEnabled(true);

        mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "web");

        WebChromeClient wvcc= new WebChromeClient(){

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                toolbar.setTitle(title);
            }
        };

        mWebView.setWebChromeClient(wvcc);
        mWebView.loadUrl(url);
    }

    class MyJavaScriptInterface {

        Context mContext;

        MyJavaScriptInterface(Context context) {
            this.mContext=context;
        }


        public void clickOnAndroid(String id) {
            ToastUtils.with(MyApplication.getAppContext()).show("OK"+id);
        }

        //点击前往商场详情页面
        @JavascriptInterface
        public void clickGoProduct(String pid){
            Intent intent =new Intent(mContext, DetailActivity.class);
            String productId = pid;
            intent.putExtra("productId",productId);
            startActivity(intent);
        }
    }

}
